package com.hiwan.unions.activity.main.fragment.fragmentController;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.hiwan.unions.activity.main.fragment.EventFragment;
import com.hiwan.unions.activity.main.fragment.EventWebFragment;
import com.hiwan.unions.activity.main.fragment.HomeFragment;
import com.hiwan.unions.activity.main.fragment.MyFragment;
import com.hiwan.unions.activity.main.fragment.NearbyFragment;
import com.hiwan.unions.activity.main.fragment.WorkersFragment;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;

import java.util.ArrayList;

import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by llg on 2017/6/20.
 */
public class FragmentController {
    private int containerId;
    private FragmentManager fm;
    private ArrayList<Fragment> fragments;
    private static FragmentController controller;
    private NearbyFragment nearbyFragment;
    private HomeFragment homeFragment;
    private EventWebFragment eventWebFragment;

    public static FragmentController getInstance(SupportActivity activity, int containerId) {
        if (controller == null) {
            controller = new FragmentController(activity, containerId);
        }
        return controller;
    }

    private FragmentController(SupportActivity activity, int containerId) {
        this.containerId = containerId;
        fm = activity.getSupportFragmentManager();
        initFragment();
    }

    private void initFragment() {
        nearbyFragment = new NearbyFragment();
        eventWebFragment = new EventWebFragment();
        homeFragment = new HomeFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(homeFragment);
        fragments.add(eventWebFragment);
        fragments.add(nearbyFragment);
        fragments.add(new WorkersFragment());
        fragments.add(new MyFragment());
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            ft.add(containerId, fragment);
        }
        ft.commitAllowingStateLoss();
    }

    public void showFragment(int position) {
        hideFragments();
        Fragment fragment = fragments.get(position);
        FragmentTransaction ft = fm.beginTransaction();
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

    public void hideFragments() {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                ft.hide(fragment);
            }
        }
        ft.commitAllowingStateLoss();
    }

    public void onDestroy() {
        controller = null;
    }

    public void cityName() {
        nearbyFragment.cityName();
        eventWebFragment.cityName();
    }

    public void cityNames() {
        homeFragment.cityName();
    }

    public Fragment getFragment(int position) {
        return fragments.get(position);
    }

}
